/*DONE* Table [dbo].[<CompanyName>$Member Sales Entry$5ecfc871-5d82-43f1-9c54-59685e82318d]  1/1  Script Date: 29.7.2020 - JI ******/
/*Add prestaging table for one specific company*/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

DECLARE @CreateTable as VARCHAR(MAX)
DECLARE @Company as VARCHAR(100)
DECLARE @CompanyID as INT

SET @CompanyID = X --The X is the value that needs to be replaced with the [Company] ID from LSInsight$Companies
SET @Company = (SELECT LSInsight$Companies.[CompanyPrefix] from dbo.LSInsight$Companies WHERE [Company] = @CompanyID)

IF @Company IS NOT NULL
BEGIN

DECLARE @DropTable as VARCHAR(MAX)
SET @DropTable = 'DROP TABLE IF EXISTS [dbo].['+@Company+'$Member Sales Entry$5ecfc871-5d82-43f1-9c54-59685e82318d]'
EXEC(@DropTable)

SET @CreateTable = 'CREATE TABLE [dbo].['+@Company+'$Member Sales Entry$5ecfc871-5d82-43f1-9c54-59685e82318d](
	[timestamp] [timestamp] NOT NULL,
	[Entry No_] [int] NOT NULL,
	[Line No_] [int] NOT NULL,
	[Source Type] [int] NOT NULL,
	[Document No_] [nvarchar](20) NOT NULL,
	[Member Account No_] [nvarchar](20) NOT NULL,
	[Member Contact No_] [nvarchar](20) NOT NULL,
	[Member Card No_] [nvarchar](100) NOT NULL,
	[Member Club] [nvarchar](10) NOT NULL,
	[Member Scheme] [nvarchar](10) NOT NULL,
	[Quantity] [decimal](38, 20) NOT NULL,
	[Net Amount] [decimal](38, 20) NOT NULL,
	[Gross Amount] [decimal](38, 20) NOT NULL,
	[Discount Amount] [decimal](38, 20) NOT NULL,
	[Cost Amount] [decimal](38, 20) NOT NULL,
	[Points Type] [int] NOT NULL,
	[Points] [decimal](38, 20) NOT NULL,
	[Date] [datetime] NOT NULL,
	[Description] [nvarchar](100) NOT NULL,
	[Item No_] [nvarchar](20) NOT NULL,
	[Item Variant Code] [nvarchar](10) NOT NULL,
	[Item Category Code] [nvarchar](20) NOT NULL,
	[Retail Product Code] [nvarchar](20) NOT NULL,
	[Store No_] [nvarchar](10) NOT NULL,
	[POS Terminal No_] [nvarchar](10) NOT NULL,
	[Transaction No_] [int] NOT NULL,
	[Store Name Int] [nvarchar](100) NOT NULL,
	[Replication Counter] [int] NOT NULL,
	[$systemId] [uniqueidentifier] NOT NULL,
 CONSTRAINT ['+@Company+'$Member Sales Entry$5ecfc871-5d82-43f1-9c54-59685e82318d$Key1] PRIMARY KEY CLUSTERED 
(
	[Entry No_] ASC,
	[Line No_] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT ['+@Company+'$Member Sales Entry$5ecfc871-5d82-43f1-9c54-59685e82318d$$systemId] UNIQUE NONCLUSTERED 
(
	[$systemId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]'

EXEC(@CreateTable)

END